<?php

	class Users {

		function __construct(){
			FunctionsHelper::includeFiles(
				array(
					ADMINISTRATOR_MODEL_DIRECTORY . 'model.php',
					ADMINISTRATOR_MODEL_DIRECTORY . 'user.php'
				)
			);
		}

		public function setLoggedUser($user){		
			if (session_id() == "")
  				session_start();
				
			$_SESSION['ATT_user_id'] = $user->getUserId();;
	}

		public function getLoggedUser(){
			if (session_id() == "")
  				session_start();
	
			if (!isset($_SESSION['ATT_user_id']) || $_SESSION['ATT_user_id'] == 0) {
				return new User();
			}
			
			$loggedUser = new User(array('user_id' => $_SESSION['ATT_user_id']));
			
			$userData = $loggedUser->getUser();
			return new User($userData[0]);				
		}

		public function isLoggedUser(){
			if (session_id() == "")
  				session_start();

  			return isset($_SESSION['ATT_user_id']);
		}

		public function userExists($arrLoginData = null){
			if (empty($arrLoginData))
				return new User();

			$user = new User(
				array(
					'username' => $arrLoginData['txtUsername'],
					'password' => $arrLoginData['txtPassword']
				)
			);

			$userExists = $user->getActiveUser();
			
			return new User($userExists);
		}

		public function LogOut(){
			if (session_id() == "")
  				session_start();

  			unset($_SESSION['ATT_user']);
  			return 1;
		}
		
		public function deleteUser($id){
			$user = new User(
				array (
					'user_id' => $id
				)
			);
			
			return $user->delete();
		}
		
		public function activateUser($id){
			$user = new User(
				array (
					'user_id' => $id
				)
			);
			
			return $user->activate();
		}
		
		public function getAll(){
			$arrUsers = array();
			
			$arrResult = User::getAll();
			foreach($arrResult as $user){
				$arrUsers[] = new User($user);
			}

			return $arrUsers;
		}
		
		public function getUser($arrInitialValue = null){
			if (empty($arrInitialValue))
				return new User();

			$user = new User($arrInitialValue);

			$user = $user->getUser();

			if (empty($user))
				return new User();
			
			return new User($user[0]);
		}
		
		public function createUser($arrInitialValues = null){
			if (empty($arrInitialValues))
				return array();

			$user = new User($arrInitialValues);
			$user->setName(utf8_decode($user->getName()));
			$user->setSurname(utf8_decode($user->getSurname()));
			
			
			if (!$this->equalPasswordAndConfirmation($arrInitialValues))
				return 0;
			
			return $user->create();
		}
		
		public function updateUser($arrInitialValues = null){
			if (empty($arrInitialValues))
				return array();

			$Myself = $this->getLoggedUser()->getUserId() == $arrInitialValues['user_id'];
			if ($Myself)
				$arrInitialValues['active'] = 1;

			$user = new User($arrInitialValues);
			$user->setName(utf8_decode($user->getName()));
			$user->setSurname(utf8_decode($user->getSurname()));

			if ($this->toChangePassword($arrInitialValues) && $this->equalPasswordAndConfirmation($arrInitialValues)){
				$user->updatePassword();
			}

			$return = $user->update();
			
			if ($Myself)
				$this->setLoggedUser($user);
			
			return $return;
		}
		
		private function toChangePassword($arrInitialValues){
			return isset($arrInitialValues['password']) && !empty($arrInitialValues['password']) || isset($arrInitialValues['confirmationPassword']) && !empty($arrInitialValues['confirmationPassword']); 
		}
		
		private function equalPasswordAndConfirmation($arrInitialValues){
			if ($arrInitialValues['password'] != $arrInitialValues['confirmationPassword']){
				FunctionsHelper::saveErrorInSession('UserAndPasswordDontMatch', Messages::getMessage('UserAndPasswordDontMatch'));
				return 0;
			}
			
			return 1;
		}
	}

?>